package com.kc.sjwh.action;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.StringUtil;
import com.kc.sjwh.entity.bo.UserBo;
import com.kc.sjwh.entity.po.Gztpzgxhzbsj;
import com.kc.sjwh.entity.po.Gztpzxx;
import com.kc.sjwh.other.AjaxRes;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.service.GztpzxxService;
import com.kc.sjwh.service.SysService;

/**
 * 工作台组action
 * @author xianding
 * @date 2017年11月28日
 */
@Controller
@RequestMapping("gztpzxx")
public class GztpzxxAction extends BaseAction {
	static Logger logger = Logger.getLogger(GztpzxxAction.class);
	
	@Autowired
	GztpzxxService service;
	@Autowired
	SysService sysService;
	/**
	 * 打开列表页
	 * @param req
	 * @return
	 */
	@RequestMapping("list")
	public String list(HttpServletRequest req) {
		return "gztpzxx/gztpzxxList";
	}
	
	/**
	 * 分页查询
	 * @param req
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getPage")
	public String getPage(HttpServletRequest req, int pageSize, int pageNum) {
		Map<String, String> m = super.getParamMap(req);

		if(!SecurityUtils.getSubject().hasRole("admin")) {
			m.put("dwdm", getLoginUser().getSsdwdm());
		}
		
		Page<Gztpzxx> page = service.getPage(pageSize, pageNum, m);
		
//		StringBuffer sb = new StringBuffer();
//		for(Gztpzxx one : page.getList()) {//数据行
//			sb.append("<tr>");
//			sb.append("<td>"+chk(one.getJqip())+"</td>");
//			sb.append("<td>"+chk(one.getJqmc())+"</td>");
//			sb.append("<td>"+chk(one.getGzth())+"</td>");
//			sb.append("<td>"+chk(one.getGztmc())+"</td>");
//			sb.append("<td>"+chk(one.getGztzmc())+"</td>");
//			sb.append("<td>"+chk(one.getSsdwmc())+"</td>");
//			sb.append("<td>");
//			sb.append("	<a href='cfg?id="+one.getJqip()+"'>工作配置项</a> &nbsp;");
//			sb.append("	<a href='form?id="+one.getJqip()+"'>编辑</a> &nbsp;");
//			sb.append("	<a onclick=\"deletes('"+one.getJqip()+"')\">删除</a>");
//			sb.append("</td>");
//			sb.append("</tr>");
//		}
//		
//		//返回结果
//		Map data = new HashMap();
//		data.put("total", page.getTotal());
//		data.put("html", sb.toString());
		return JSON.toJSONString(new AjaxRes(page));
	}
	
	/**
	 * 获取模板
	 */
	@ResponseBody
	@RequestMapping("getModel")
	public String getModel(HttpServletRequest req) {
		String zbxbh=req.getParameter("zbxbh");
		String model=service.getModel(zbxbh);
		//返回结果
		Map data = new HashMap();
		data.put("model", model);
		return JSON.toJSONString(new AjaxRes(data));
	}
	
	/**
	 * 获取模板
	 */
	@ResponseBody
	@RequestMapping("getModelByIp")
	public String getModelByIp(HttpServletRequest req) {
		String zbxbh=req.getParameter("zbxbh");
		String jqip=req.getParameter("jqip");
		String model=service.getModelByIp(zbxbh,jqip);
		//返回结果
		Map data = new HashMap();
		data.put("model", model==null?"":model);
		return JSON.toJSONString(new AjaxRes(data));
	}
	
	/**
	 * 打开表单(新增或修改)
	 * @param id
	 */
	@RequestMapping("form")
	public String form(HttpServletRequest req, String id) {
		req.setAttribute("act", id==null?"add":"update");
		req.setAttribute("actName", id==null?"新增":"修改");
		if (StringUtil.isNotEmpty(req.getParameter("gztzbh"))) {
			Gztpzxx one=new Gztpzxx();
			one.setGztzbh(req.getParameter("gztzbh"));
			String string=req.getParameter("gztzmc");
			try {
				string = new String(string.getBytes("ISO-8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			one.setGztzmc(string);
			req.setAttribute("one", one);
		}else {			
			req.setAttribute("one", service.form(id));
		}
		req.setAttribute("loginUser", super.getLoginUser());
		return "gztpzxx/gztpzxxForm";
	}
	
	/**
	 * 配置工作台(新增或修改)
	 * @param id
	 */
	@RequestMapping("cfg")
	public String conf(HttpServletRequest req, String id) {
		req.setAttribute("pzx", sysService.getAllGztpzxAsDict());
		req.setAttribute("one", service.form(id));
		req.setAttribute("loginUser", super.getLoginUser());
		return "gztpzxx/gztpzxxCfg";
	}
	
	/**
	 * 保存（新增或修改）
	 * @param one
	 * @param act add/update
	 */
	@ResponseBody
	@RequestMapping("save")
	public String save(Gztpzxx one, String act) {
		if("add".equals(act)) {
			UserBo user = getLoginUser();
			one.setCjr(user.getMc());
			if(StringUtils.isEmpty(one.getSsdwdm())) {
				one.setSsdwdm(user.getSsdwdm());
				one.setSsdwmc(user.getSsdwmc());
			}
			service.add(one);
		} else {
			one.setGxr(getLoginUser().getMc());
			service.update(one);
		}
		return JSON.toJSONString(new AjaxRes());
	}
	
	/**
	 * 保存配置（新增或修改）
	 * @param one
	 * @param act add/update
	 */
	@ResponseBody
	@RequestMapping("saveCfg")
	public String saveCfg(Gztpzgxhzbsj one, String act) {
		Gztpzgxhzbsj up=service.getCfg(one);
		if(up==null) {
			UserBo user = getLoginUser();
			one.setCjr(user.getMc());
			service.addCfg(one);
		} else {
			one.setGxr(getLoginUser().getMc());
			service.updateCfg(one,up.getBh());
		}
		return JSON.toJSONString(new AjaxRes());
	}
	
	/**
	 * 删除
	 * @param req
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String delete(HttpServletRequest req, String id) {
		service.delete(id);
		return JSON.toJSONString(new AjaxRes());
	}
}
